﻿window.onload = function () {
    window.App = new Vue({
        el: "#app",
        data: {
            CompanyId: null,
            Cycle: null,
            Data: []
        },
        methods: {
            //初始化
            INIT: function () {
                var that = this;
                if (that.QueryString("CompanyId") == null || that.QueryString("CompanyId") == "") {
                    window.location.href = "/Content/App/html/erro.html";
                    return;
                }
                that.CompanyId = parseInt(that.QueryString("CompanyId"));
                that.Cycle = parseInt(that.QueryString("Cycle"));
                that.Ajax({
                    asyn: false,
                    data: {
                        ApplyId: that.ApplyId
                    },
                    success: function (result) {
                        if (result.StatusNumber!=1) {
                            window.location.href = "/Content/App/html/erro.html";
                        }
                        var data = result.Data;
                        var _data = [];
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];
                            _data.push({
                                Code: current.N_PROID.toString(),
                                ParentCode: current.N_PARENTID == null ? "" : current.N_PARENTID.toString(),
                                Name: current.C_RPONAME,
                                JzMoney: current.N_MONTHAMOUNT == null ? "0" : current.N_MONTHAMOUNT.toString(),
                                JzUseMoney: current.N_SYAMOUNT == null ? "0" : current.N_SYAMOUNT.toString(),
                                ApplyMoney: current.N_AMOUNT1 == null ? "0" : current.N_AMOUNT1.toString(),
                                ApplyRemark: current.C_REMARK == null ? "" : current.C_REMARK,
                                AssMoney: current.N_AMOUNT == null ? "0" : current.N_AMOUNT1.toString(),
                                AssRemark: current.C_REMARK == null ? "" : current.C_REMARK,
                                Type: current.N_TYPE
                            });
                        }
                        that.Data = _data;
                    },
                    error: function () {
                        window.location.href = "/Content/App/html/erro.html";
                    }
                });
            },
            //保存数据
            SAVE_CLICK: function () {
                var that = this;
                var data = {
                    ID: that.ApplyId,
                    Data: [],
                    MData: []
                };
                data.Data = [];
                for (var i = 0; i < that.Data.length; i++) {
                    var current = that.Data[i];
                    if (current.Type != 2 && current.Type != 4) {
                        continue;
                    }
                    data.Data.push({
                        PRO_NUMBER: current.Code,
                        APPLY_MONEY: current.Money,
                        REMARK: current.Remark
                    });
                }
                data.MData = [];
                for (var i = 0; i < that.WeekData.length; i++) {
                    var current = that.WeekData[i];
                    var obj = {};
                    data.MData.push(obj);
                    obj.ID = current.Id;
                    obj.Data = [];
                    for (var z = 0; z < that.WeekData[i].Data.length; z++) {
                        var currentZ = that.WeekData[i].Data[z];
                        obj.Data.push({
                            PRO_NUMBER: currentZ.PRO_NUMBER,
                            APPLY_MONEY: currentZ.N_AMOUNT,
                            REMARK: ""
                        });
                    }
                }
                var option = {
                    url: "/Apply1/Save",
                    data: data,
                    success: function (result) {
                        if (result.StatusNumber == 1) {
                            alert("保存成功");
                            //that.INIT();
                        }
                        else {
                            console.log(result);
                            alert(result.StatusMsg);
                        }
                    },
                    erro: function () {
                        alert("接口异常");
                    }
                };
                that.Ajax(option);
            },
            MONEY_CHANGE: function (event, code) {
                
                //排序+递归  从顶级开始算
                function Sort(data) {
                    for (var i = 0; i < data.length; i++) {
                        if (i + 1 == data.length) {
                            continue;
                        }
                        var current = data[i];
                        var last = data[i + 1];
                        if (current.OrderBy > last.OrderBy) {
                            data[i + 1] = current;
                            data[i] = last;
                            Sort(data);
                            break;
                        }
                    }
                }
                
                function ConS(data, parentCode) {
                    if (parentCode == undefined) {
                        parentCode = null;
                    }
                    var arry = [];
                    for (var i = 0; i < data.length; i++) {
                        var current = data[i];
                        if (current.ParentCode == parentCode) {
                            arry.push(current);
                        }
                    }
                    //调用排序
                    Sort(arry);
                    //
                    var _arry = [];
                    for (var i = 0; i < arry.length; i++) {
                        var current = arry[i];
                        _arry.push(current);
                        var arry1 = ConS(data, current.Code);
                        for (var z = 0; z < arry1.length; z++) {
                            _arry.push(arry1[z]);
                        }
                    }
                    return _arry;
                }
                //累加基层 从顶级开始算
                function LeiJia(data, parentCode) {
                    if (parentCode == undefined) {
                        parentCode = null;
                    }
                    var sum = 0;
                    for (var i = 0; i < data.length; i++) {
                        var current = data[i];
                        if (current.ParentCode == parentCode) {
                            var _sum;
                            //如果为可填项或者子项就获取值
                            if (current.Type == 2 || current.Type == 4) {
                                _sum = current.Money;
                            }
                            else {
                                _sum = LeiJia(data, current.Code);
                                current.Money = _sum;
                            }
                            sum = that.Float.add(sum, _sum);
                        }
                    }
                    return sum;
                }
                //层级算法 从底层开始算
                function LevGo(data, parentCode, lev) {
                    if (parentCode == undefined) {
                        parentCode = null;
                        lev = 0;
                    }
                    lev++;
                    for (var i = 0; i < data.length; i++) {
                        var current = data[i];

                        if (current.ParentCode == parentCode) {
                            //console.log(lev);
                            var str = "";
                            for (var k = 0; k < lev; k++) {
                                if (lev == 1) {
                                    break;
                                }
                                str += "\xa0\xa0";
                            }
                            str += current.Name.trim();
                            current.Name = str;
                            current.Lev = lev;
                            //console.log(current.Name + "。Lev:" + lev);
                            LevGo(data, current.Code, lev);
                        }
                    }
                }
            },
            //组装子项
            INIT_Z: function () {
                var that = this;
                var data = [];
                var mData = [];
                {
                    //组装模型
                    //装载周信息
                    for (var i = 0; i < that.WeekData.length; i++) {
                        var current = that.WeekData[i];
                        mData.push({
                            Index: current.Index,
                            Id: current.Id
                        });
                    }
                    //console.log(mData);
                    //主项添加进入模型
                    for (var i = 0; i < that.ProData.length; i++) {
                        var current = that.ProData[i];
                        var entity = {
                            Code: current.PRO_NUMBER,
                            ParentCode: current.PRO_PARENT_NUMBER,
                            Name: current.PRO_NAME,
                            Money: 0,
                            Remark: "",
                            Type: current.PRO_TYPE,
                            OrderBy: current.PRO_ORDER_BY,
                            JzMoney: 0,
                            JzUseMoney: 0
                        };
                        data.push(entity);
                    }

                    //console.log("xxx");
                    //子项添加进入模型
                    for (var i = 0; i < that.MonthData.length; i++) {
                        var current = that.MonthData[i];
                        //console.log("xx");
                        //console.log(current.PRO_NUMBER);
                        //console.log("L:"+current.PRO_NUMBER.split('-').length);
                        if (current.PRO_NUMBER.split('-').length == 1) {
                            for (var z = 0; z < data.length; z++) {
                                if (current.PRO_NUMBER == data[z].Code) {
                                    data[z].Money = current.N_AMOUNT;
                                    data[z].Remark = current.C_REMARK;
                                }
                            }
                            continue;
                        }
                        var entity = {
                            Code: current.PRO_NUMBER,
                            ParentCode: current.PRO_NUMBER.split('-')[0],
                            Name: current.PRO_NAME,
                            Money: current.N_AMOUNT,
                            Remark: current.C_REMARK,
                            Type: 4,
                            OrderBy: 0,
                            JzMoney: 0,
                            JzUseMoney: 0
                        };
                        data.push(entity);
                    }
                    //加载截止数据
                    for (var i = 0; i < data.length; i++) {
                        var current = data[i];
                        for (var z = 0; z < that.JzData.length; z++) {
                            if (current.Code == that.JzData[z].C_PRONUM) {
                                current.JzMoney = that.JzData[z].N_AMOUNT;
                                current.JzUseMoney = that.JzData[z].N_USEAMOUNT;
                                break;
                            }
                        }
                    }
                    console.log(data);
                    //计算累计月数据
                    for (var i = 0; i < data.length; i++) {
                        var current = data[i];
                        if (current.Type == 2 || current.Type == 4) {
                            var sum = 0;
                            for (var z = 0; z < that.WeekData.length; z++) {
                                for (var k = 0; k < that.WeekData[z].Data.length; k++) {
                                    if (that.WeekData[z].Data[k].PRO_NUMBER == current.Code) {
                                        //sum += that.WeekData[z].Data[k].N_AMOUNT;
                                        sum = that.Float.add(sum, that.WeekData[z].Data[k].N_AMOUNT);
                                        //sum = that.Float.add(sum, _sum);
                                        break;
                                    }
                                }
                            }
                            //console.log("累计值：" + sum);
                            current.Money = sum;
                            continue;
                        }
                    }
                    //console.log(data);
                    //调用排序算法
                    data = ConS(data);
                    //console.log(data);
                    //return;
                    //console.log(data);
                    //调用累加算法
                    LeiJia(data);
                    LeiJia1(data);
                    LeiJia2(data);
                    //调用层级算法
                    LevGo(data);
                    //排序+递归  从顶级开始算
                    function Sort(data) {
                        for (var i = 0; i < data.length; i++) {
                            if (i + 1 == data.length) {
                                continue;
                            }
                            var current = data[i];
                            var last = data[i + 1];
                            if (current.OrderBy > last.OrderBy) {
                                data[i + 1] = current;
                                data[i] = last;
                                Sort(data);
                                break;
                            }
                        }
                    }
                    function ConS(data, parentCode) {
                        if (parentCode == undefined) {
                            parentCode = null;
                        }
                        var arry = [];
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];
                            if (current.ParentCode == parentCode) {
                                arry.push(current);
                            }
                        }
                        //调用排序
                        Sort(arry);
                        //
                        var _arry = [];
                        for (var i = 0; i < arry.length; i++) {
                            var current = arry[i];
                            _arry.push(current);
                            var arry1 = ConS(data, current.Code);
                            for (var z = 0; z < arry1.length; z++) {
                                _arry.push(arry1[z]);
                            }
                        }
                        return _arry;
                    }
                    //累加基层 从顶级开始算
                    function LeiJia(data, parentCode) {
                        if (parentCode == undefined) {
                            parentCode = null;
                        }
                        var sum = 0;
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];
                            if (current.ParentCode == parentCode) {
                                var _sum;
                                //如果为可填项或者子项就获取值
                                if (current.Type == 2 || current.Type == 4) {
                                    _sum = current.Money;
                                }
                                else {
                                    _sum = LeiJia(data, current.Code);
                                    current.Money = _sum;
                                }
                                sum = that.Float.add(sum, _sum);
                            }
                        }
                        return sum;
                    }
                    //1累加基层 从顶级开始算
                    function LeiJia1(data, parentCode) {
                        if (parentCode == undefined) {
                            parentCode = null;
                        }
                        var sum = 0;
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];
                            if (current.ParentCode == parentCode) {
                                var _sum;
                                //如果为可填项或者子项就获取值
                                if (current.Type == 2 || current.Type == 4) {
                                    _sum = current.JzMoney;
                                }
                                else {
                                    _sum = LeiJia1(data, current.Code);
                                    current.JzMoney = _sum;
                                }
                                sum = that.Float.add(sum, _sum);
                            }
                        }
                        return sum;
                    }
                    //2累加基层 从顶级开始算
                    function LeiJia2(data, parentCode) {
                        if (parentCode == undefined) {
                            parentCode = null;
                        }
                        var sum = 0;
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];
                            if (current.ParentCode == parentCode) {
                                var _sum;
                                //如果为可填项或者子项就获取值
                                if (current.Type == 2 || current.Type == 4) {
                                    _sum = current.JzUseMoney;
                                }
                                else {
                                    _sum = LeiJia2(data, current.Code);
                                    current.JzUseMoney = _sum;
                                }
                                sum = that.Float.add(sum, _sum);
                            }
                        }
                        return sum;
                    }
                    //层级算法 从底层开始算
                    function LevGo(data, parentCode, lev) {
                        if (parentCode == undefined) {
                            parentCode = null;
                            lev = 0;
                        }
                        lev++;
                        for (var i = 0; i < data.length; i++) {
                            var current = data[i];

                            if (current.ParentCode == parentCode) {
                                //console.log(lev);
                                var str = "";
                                for (var k = 0; k < lev; k++) {
                                    if (lev == 1) {
                                        break;
                                    }
                                    str += "\xa0\xa0";
                                }
                                str += current.Name.trim();
                                current.Name = str;
                                current.Lev = lev;
                                //console.log(current.Name + "。Lev:" + lev);
                                LevGo(data, current.Code, lev);
                            }
                        }
                    }
                }
                that.Data = data;
                that.MData = mData;
            }
        },
        mounted: function () {
            var that = this;
            //console.log(top.Helper);
            //注入一个Ajax
            that.Ajax = top.Helper.Current.Ajax;
            //注入一个用户信息
            that.User = top.Helper.Current.User;
            //注入一个QueryString
            that.QueryString = new top.Helper.Url.Url(window).QueryString;
            //异步加载截止数据
            that.GetJzData = function (companyId, monthStr, callback) {
                var option = {
                    url: "/Apply1/GetJzLastMonth",
                    data: {
                        CompanyId: companyId,
                        ApplyMonth: monthStr
                    },
                    success: function (result) {
                        if (result.StatusNumber == 1) {
                            callback(result.Data);
                        }
                        else {
                            alert(result.StatusMsg);
                        }
                        //console.log(result);
                    },
                    result: function () {
                        alert("接口异常");
                    }
                };
                that.Ajax(option);
            };
            //异步加载资金申请信息
            that.GetApplyData = function (applyId, callback) {
                var option = {
                    url: "/ApplyMonth/Get",
                    data: {
                        ID: applyId
                    },
                    success: function (result) {
                        if (result.StatusNumber == 1) {
                            //console.log(result);
                            callback(result.Data);
                        }
                        else {
                            alert(result.StatusMsg);
                        }
                        //console.log(result);
                    },
                    result: function () {
                        alert("接口异常");
                    }
                };
                that.Ajax(option);
            }
            that.Float = top.Helper.Float;
            //调用初始化
            that.INIT();

            
        }
    });
}